//
//  WPRegisterViewController.m
//  wobingwoyi
//
//  Created by WGP on 16/7/30.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPRegisterViewController.h"
#import "WPMineLoginVC.h"
#import "WPRegisterView.h"
#import "NSString+Hash.h"

@interface WPRegisterViewController ()

@property(nonatomic,strong)UIView *navBarView;
@property(nonatomic,strong)UITextField *inviteCodeField;

@end

@implementation WPRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.view.backgroundColor = RGB_SAME(245);
    
    [self setupUI];
    
    [self.view endEditing:YES];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self dismiss];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    [self.registerView endEditing:YES];
}

-(void)setupUI
{
    [self addNavBarView];
    
    [self setupOtherSubviews];
}

-(void)addNavBarView
{
    UIView *navBarView = [UIView new];
    navBarView.backgroundColor = [UIColor whiteColor];
    self.navBarView = navBarView;
    [self.view addSubview:navBarView];
    
    WPBtnNoHighLight *backBtn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchDown];
    [navBarView addSubview:backBtn];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"注册";
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = RGB_SAME(51);
    self.titleLabel = titleLabel;
    [titleLabel sizeToFit];
    
    NSMutableDictionary *attrDic = [NSMutableDictionary dictionary];
    attrDic[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    
    CGSize strSize = [titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil].size;
    [navBarView addSubview:titleLabel];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = GLobalLineColor;
    [self.view addSubview:lineView];
    
    [navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(navBarView).offset(8);
        make.bottom.equalTo(navBarView).offset(-8);
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBarView);
        make.top.equalTo(navBarView).offset(20+(44-strSize.height)*0.5);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.view).offset(64);
    }];
}

-(void)setupOtherSubviews
{
    WPRegisterView *registerInfoView = [WPRegisterView new];
    self.registerView = registerInfoView;
    [self.view addSubview:registerInfoView];
    
    UIView *bgView = [UIView new];
    self.inviteCodeFieldView = bgView;
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.borderColor = GLobalLineColor.CGColor;
    bgView.layer.borderWidth = 0.5;
    [self.view addSubview:bgView];
    
    UITextField *inviteCodeField = [UITextField new];
    inviteCodeField.placeholder = @"输入邀请码(可填)";
    inviteCodeField.font = [UIFont systemFontOfSize:14];
    self.inviteCodeField = inviteCodeField;
    [bgView addSubview:inviteCodeField];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registerBtn = registerBtn;
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted];
    [registerBtn setTitle:@"注册登录" forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [registerBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:registerBtn];
    
    [registerInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.navBarView.mas_bottom).offset(10*SCALE);
        make.height.mas_equalTo(3*44*SCALE);
    }];
    
    
    CGSize strSize = [inviteCodeField.placeholder sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:14];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(registerInfoView.mas_bottom).offset(10*SCALE);
        make.width.mas_equalTo(strSize.width+20);
        make.height.mas_equalTo(30*SCALE);
    }];
    
    
    [inviteCodeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(10);
        make.right.equalTo(bgView).offset(-10);
        make.top.bottom.equalTo(bgView);
    }];
    
    
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(registerInfoView.mas_bottom).offset(84*SCALE);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
}

-(void)btnDidClick:(UIButton *)btn
{
 
    NSString *phoneNum = self.registerView.phoneNumField.text;
    NSString *verifyCode = self.registerView.verifyField.text;
    
    if (!verifyCode.length)
    {
        [self showErrorInfoWithStatus:@"请填写验证码"];
        return;
    }
    
    if (!self.registerView.passwordField.text.length || (self.registerView.passwordField.text.length < 6))
    {
        [self showErrorInfoWithStatus:@"请输入6~20位密码"];
        return;
    }
    
    if(self.inviteCodeField.text.length)
    {
        if (self.inviteCodeField.text.length != 6)
        {
            [self showErrorInfoWithStatus:@"邀请码为6位"];
            return ;
        }
    }
    

    NSString *password = [self.registerView.passwordField.text md5String];
    NSString *inviteCode = self.inviteCodeField.text;
    
    NSString *url = @"user/register.do";
    //获取设备的唯一标示
    NSString *uniqueStr = [self getsTheUniqueIdentifierForTheDevice];
    NSLog(@"唯一标示:%@",uniqueStr);
    NSDictionary *parameters = @{@"account":phoneNum,@"password":password,@"invitedCode":inviteCode,@"codeInput":verifyCode,@"equipmentId":uniqueStr};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
    {
        NSDictionary *resultDic = (NSDictionary *)responseObject;
        
        if([resultDic[@"result"] isEqualToString:@"success"])
        {
               [self showSuccessInfoWithStatus:@"注册成功"];
            
                NSDictionary *parameters = @{@"account":phoneNum,@"password":password};
                [[WPUserAccountViewModel sharedInstance] globalLoginWith:parameters successHandle:^{
                    
                    //                [self.navigationController popViewControllerAnimated:NO];
                    //        登录成功
                    [self.view endEditing:YES];
                    [self showSuccessInfoWithStatus:@"登陆成功"];
                    
                    
                    WPMineLoginVC *lvc = self.navigationController.viewControllers[0];
                    
                    [lvc.delegate loadUserInfomation:lvc];
                    
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                    
                }];
            
        }
        else
        {
            [self showErrorInfoWithStatus:resultDic[@"detail"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        if (error)
        {
            [self showErrorInfoWithStatus:@"注册失败"];
        }
    }];
}

-(void)backBtnDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end









